# http://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud
# wordcloud下载地址，由于pycharm有可能安装不上
# 下载方式，从网站上下载.whl文件，将该文件复制到该项目的根目录下，然后再Terminal中:
#     pip install .\wordcloud-1.8.1-cp37-cp37m-win_amd64.whl

import jieba #分词
from matplotlib import pyplot as plt #绘图
from wordcloud import WordCloud #词云
from PIL import Image #图片处理
import numpy as np
import sqlite3


# 准备数据
con = sqlite3.connect("../Script/douban.db")
cur = con.cursor()
sql = "select Details from douban_movies"
data = cur.execute(sql)
text = ""
for item in data:
    text = text + item[0]
    # print(item[0])
print(text)
cur.close()
con.close()

print("分词")

# 分词
cut = jieba.cut(text)
string = ' '.join(cut)
print(string)
print(len(string))

img = Image.open('../static/img/cd.png') #图片背景必须为白色
image_array = np.array(img) #将图片转成数组
WC = WordCloud(
    background_color='white',
    mask=image_array,
    font_path="rC:\Windows\Fonts\simsun.ttc" #字体所在位置 C:\Windows\Fonts
)
WC.generate_from_text(string)

# 绘制图片
fig = plt.figure(1)
plt.imshow(WC)
plt.axis('off')
# plt.show() #显示生成的词云

# 将生成的图片保存
plt.savefig("ciyun.png")